<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="width">
    <div>
      <el-form ref="" :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"></el-input>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import chapter from "../../../../api/chapter";
export default {
  data() {
    return {
      title: "新增章节",
      dialogVisible: false,
      chapter: {
        courseId: "",
        //章节对象
        title: "",
        sort: 0,
      },
    };
  },
  methods: {
    open(val, id) {
      // console.log(val, id);
      this.dialogVisible = val;
      if (id) {
        chapter.getChapterById(id).then((resp) => {
          this.chapter = resp.data.item;
          // console.log(this.chapter);
        });
      }
    },
    saveOrUpdate() {
      if (!this.chapter.id) {
        this.chapter.courseId = this.$parent.$parent.courseId;
        chapter.saveChapter(this.chapter).then((resp) => {
          if (resp) {
            this.$message.success(resp.message);
            this.dialogVisible = false;
            this.$parent.initChapterList();
            this.chapter.title = "";
            this.chapter.sort = 0;
          }
        });
      } else {
        this.title = "更新章节";
        // console.log(this.chapter.courseId);
        // this.chapter.courseId = this.$parent.$parent.courseId;
        chapter.updateChapterById(this.chapter).then((resp) => {
          if (resp) {
            this.$message.success(resp.message);
            this.dialogVisible = false;
            this.$parent.initChapterList();
            this.chapter.title = "";
            this.chapter.sort = 0;
          }
        });
      }
    },
  },
};
</script>

<style></style>
